<template>
  <div class="app-container">
    <div class="pro-header">
      <div>项目列表</div>
      <div class="pro-search">
        <el-checkbox v-model="isHide" label="隐藏无权限项目" @change="checkChange" />
        <el-input
          v-model="params.projectName"
          clearable
          size="mini"
          placeholder="搜索项目"
          suffix-icon="el-icon-search"
          @change="inputChange()"
          @keydown.enter.native="inputChangeQuery()"
        />
      </div>
    </div>
    <div class="infinite-list-wrapper content">
      <div class="project" infinite-scroll-disabled="disabled">
        <project-item v-for="(item,i) in list" :key="i" :data="item" class="item" @pushSetUp="pushSetUp" @click.native="pushProject(item)" />
      </div>
      <div class="more_button center-fixed">
        <el-button size="small" @click="cancel">收回</el-button>
        <el-button type="primary" size="small" :loading="loading" @click="load">展示更多</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { VxeListMixin } from '@/mixins/VxeListMixin'
import projectItem from '@/views/companents/projectItem/home_index.vue'

export default {
  name: 'Dashboard',
  components: {
    projectItem
  },
  mixins: [VxeListMixin],
  data() {
    return {
      searchPro: '',
      url: {
        queryList: this.$api.projectController.getProject
      },
      count: 0,
      loading: false,
      isHide: true,
      params: {
        page: 1,
        pageSize: 12,
        enable: '1',
        projectName: ''
      },
      list: []
    }
  },
  computed: {
    ...mapGetters(['name']),
    noMore() {
      return this.count >= this.list.length
    },
    disabled() {
      return this.loading || this.noMore
    }
  },
  created() {
    this.url.query = this.url.queryList
    this.load()
  },
  methods: {
    pushSetUp(item) {
      this.$router.push({ name: 'setupIndex', query: { projectId: item.projectId }})
    },
    checkChange(value) {
      this.params.page = 1
      this.params.enable = value ? '1' : null
      this.load()
    },
    inputChange(value) {
      if (!value) {
        this.params.page = 1
        this.load()
      }
    },
    inputChangeQuery() {
      this.params.page = 1
      this.load()
    },
    pushProject(item) {
      this.$router.push({ name: 'recordIndex', params: { projectId: item.projectId }})
    },
    cancel() {
      this.params.page = 1
      this.load()
    },
    load() {
      this.loading = true
      this.query((res) => {
        this.loading = false
        if (this.params.page !== 1 && (res.data == null || res.data.length === 0)) {
          return this.$message.warning('没有更多信息了')
        }

        this.list = res.data
        const step = this.params.pageSize
        if ((this.list.length - this.count) <= step) {
          this.count = this.list.length
          this.params.page++
        } else if ((this.list.length - this.count) / step >= 0) {
          this.params.page++
          this.count += step
          console.log(this.params)
        }
      }).catch((err) => {
        console.log(err)
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
.pro-header {
  display: flex;
  justify-content: space-between;
  .pro-search {
    display: flex;
    align-items: center;
    .el-checkbox {
      margin-right: 10px;
    }
    ::v-deep .el-input--mini .el-input__inner {
      background-color: #f5f7fa;
    }
  }
}
.content{
  height: calc(100vh - 160px);
  overflow: auto;
}
.project {
  display: flex;
  flex-wrap: wrap;
  margin-top: 20px;
  .item {
    flex: 0 0 25%; /* 每个div占用20%的宽度 */
  }
}
.more_button{
  background-color: white;
  transition: opacity 1.5s;
  // display: flex;
  // align-items: center;
  // justify-content: center;
  // margin-top: 20px;
}
.center-fixed {
  position: fixed;
  left: 50%;
  right: 50%;
  bottom: 10px;
  margin: auto;
  width: 180px;  /* 设置元素的宽度 */
  height: 50px;  /* 设置元素的高度 */
}
</style>
